﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LinWon.Core.Model
{
    /// <summary>
    /// 用户
    /// </summary>
    public class User
    {
        #region
        private int _autoid;
        /// <summary>
        /// 用户编号
        /// </summary>
        public int AutoID
        {
            get { return _autoid; }
            set { _autoid = value; }
        }
        private int _memberid;
        /// <summary>
        /// 会员编号
        /// </summary>
        public int MemberID
        {
            get { return _memberid; }
            set { _memberid = value; }
        }
        private string _name = string.Empty;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _gender = string.Empty;
        /// <summary>
        /// 性别
        /// </summary>
        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }
        private DateTime _birthday;
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
        private string _tel = string.Empty;
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Tel
        {
            get { return _tel; }
            set { _tel = value; }
        }
        private string _mobile = string.Empty;
        /// <summary>
        /// 移动电话
        /// </summary>
        public string Mobile
        {
            get { return _mobile; }
            set { _mobile = value; }
        }
        private string _email = string.Empty;

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }
        private string _idcode = string.Empty;
        /// <summary>
        /// 身份证号码
        /// </summary>
        public string IDCode
        {
            get { return _idcode; }
            set { _idcode = value; }
        }
        private DateTime _createtime;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime
        {
            get { return _createtime; }
            set { _createtime = value; }
        }
        private UserState _state;
        /// <summary>
        /// 状态
        /// </summary>
        public UserState State
        {
            get { return _state; }
            set { _state = value; }
        }
        private string _lgname = string.Empty;
        /// <summary>
        /// 登录名
        /// </summary>
        public string LgName
        {
            get { return _lgname; }
            set { _lgname = value; }
        }
        private string _pwd = string.Empty;
        /// <summary>
        /// 密码
        /// </summary>
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }
        private string _remark = string.Empty;
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark
        {
            get { return _remark; }
            set { _remark = value; }
        }
        #endregion

        #region sql
        private const string select = "SELECT AutoID, MemberID, [Name], Gender, Brithday, Tel, Mobile, Email, IDCode, CreateTime, [State], Remark "
                                    + "FROM LW_User WHERE AutoID=@AutoID ";
        private const string insert = "pUserAdd";
        
        private const string changepwd = "UPDATE LW_Client SET LgnPwd=@LgnPwd WHERE UserID=@UserID";
        private const string modifybaseinfo = "UPDATE LW_User SET [Name]=@Name,Gender=@Gender,Brithday=@Brithday,Tel=@Tel,Mobile=@Mobile,Email=@Email,IDCode=@IDCode,Remark=@Remark WHERE AutoID=@AutoID";
        #endregion

        #region 
        /// <summary>
        /// 用户
        /// </summary>
        public User() { }

        /// <summary>
        /// 用户
        /// </summary>
        /// <param name="id">用户号</param>
        public User(int id)
        {
            System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@AutoID", System.Data.SqlDbType.Int, 4);
            p.Value = id;

            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

                dr = helper.ExecuteReader(select, p);

                if (dr.Read())
                {
                    this.FillByDataReader(dr);
                }
            }
            finally
            {
                if (null != dr && !dr.IsClosed)
                    dr.Close();
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            System.Data.SqlClient.SqlParameter[] parms = {
                new System.Data.SqlClient.SqlParameter("@MemberID",System.Data.SqlDbType.Int,4),
                new System.Data.SqlClient.SqlParameter("@Name",System.Data.SqlDbType.VarChar,10),
                new System.Data.SqlClient.SqlParameter("@Gender",System.Data.SqlDbType.VarChar,2),
                new System.Data.SqlClient.SqlParameter("@Brithday",System.Data.SqlDbType.DateTime),
                new System.Data.SqlClient.SqlParameter("@Tel",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Mobile",System.Data.SqlDbType.VarChar,15),
                new System.Data.SqlClient.SqlParameter("@Email",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@IDCode",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Remark",System.Data.SqlDbType.VarChar,200),
                new System.Data.SqlClient.SqlParameter("@LgnName",System.Data.SqlDbType.VarChar,10),                
                new System.Data.SqlClient.SqlParameter("@LgnPwd",System.Data.SqlDbType.VarChar,32)};

            int index = 0;

            parms[index++].Value = this.MemberID;

            if (!string.IsNullOrEmpty(this.Name))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Name;

            if (!string.IsNullOrEmpty(this.Gender))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Gender;

            if (DateTime.MinValue==this.Birthday)
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Birthday;

            if (!string.IsNullOrEmpty(this.Tel))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Tel;

            if (!string.IsNullOrEmpty(this.Mobile))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Mobile;

            if (!string.IsNullOrEmpty(this.Email))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Email;

            if (!string.IsNullOrEmpty(this.IDCode))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.IDCode;

            if (!string.IsNullOrEmpty(this.Remark))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Remark;
                        
            parms[index++].Value = this.LgName;           
            parms[index++].Value = this.Pwd;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            int result = helper.RunProcReturnInt(insert, parms);
            this.AutoID = result;
            return result;
        }

        /// <summary>
        /// 填充用户信息,SELECT必须按顺序(不会打开或关闭dr)
        /// </summary>
        /// <param name="dr"></param>
        public void FillByDataReader(System.Data.SqlClient.SqlDataReader dr)
        {
            int index = 0;
            this.AutoID = dr.GetInt32(index++);
            this.MemberID = dr.GetInt32(index++);

            if (!dr.IsDBNull(index)) this.Name = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Gender = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Birthday = dr.GetDateTime(index);
            index++;
            if (!dr.IsDBNull(index)) this.Tel = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Mobile = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.Email = dr.GetString(index);
            index++;
            if (!dr.IsDBNull(index)) this.IDCode = dr.GetString(index);
            index++;
            this.CreateTime = dr.GetDateTime(index++);
            index++;
            this.State = (UserState)dr.GetInt32(index++);
            index++;
            if (!dr.IsDBNull(index)) this.Remark = dr.GetString(index);
        }

        

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public int ChangePWD(string newPwd)
        {
            System.Data.SqlClient.SqlParameter[] parms = {
                new System.Data.SqlClient.SqlParameter("@LgnPwd",System.Data.SqlDbType.VarChar,32),
                new System.Data.SqlClient.SqlParameter("@UserID",System.Data.SqlDbType.Int,4)};

            parms[0].Value = newPwd;
            parms[1].Value = this.AutoID;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            return helper.ExecuteNonQuery(changepwd, parms);
        }

        /// <summary>
        /// 登录名是否已存在
        /// </summary>
        /// <returns></returns>
        public bool ExistsLoginName()
        {
            string sql = "SELECT count(1) FROM LW_Client WHERE LgnName=@LgnName";
            System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter("@LgnName", System.Data.SqlDbType.VarChar, 10);
            p.Value = this.LgName;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            object obj = helper.ExecuteScalar(sql, p);

            int result = 0;

            int.TryParse(obj.ToString(), out result);

            return result > 0;
        }

        /// <summary>
        /// 修改基本信息(不包括登录名、密码、状态、所属会员)
        /// </summary>
        /// <returns></returns>
        public int Modify()
        {
            System.Data.SqlClient.SqlParameter[] parms = {                
                new System.Data.SqlClient.SqlParameter("@Name",System.Data.SqlDbType.VarChar,10),
                new System.Data.SqlClient.SqlParameter("@Gender",System.Data.SqlDbType.VarChar,2),
                new System.Data.SqlClient.SqlParameter("@Brithday",System.Data.SqlDbType.DateTime),
                new System.Data.SqlClient.SqlParameter("@Tel",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Mobile",System.Data.SqlDbType.VarChar,15),
                new System.Data.SqlClient.SqlParameter("@Email",System.Data.SqlDbType.VarChar,50),
                new System.Data.SqlClient.SqlParameter("@IDCode",System.Data.SqlDbType.VarChar,20),
                new System.Data.SqlClient.SqlParameter("@Remark",System.Data.SqlDbType.VarChar,200),                               
                new System.Data.SqlClient.SqlParameter("@AutoID",System.Data.SqlDbType.Int,4)};

            int index = 0;            

            if (!string.IsNullOrEmpty(this.Name))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Name;

            if (!string.IsNullOrEmpty(this.Gender))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Gender;

            if (DateTime.MinValue == this.Birthday)
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Birthday;

            if (!string.IsNullOrEmpty(this.Tel))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Tel;

            if (!string.IsNullOrEmpty(this.Mobile))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Mobile;

            if (!string.IsNullOrEmpty(this.Email))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Email;

            if (!string.IsNullOrEmpty(this.IDCode))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.IDCode;

            if (!string.IsNullOrEmpty(this.Remark))
                parms[index++].Value = DBNull.Value;
            else
                parms[index++].Value = this.Remark;
            
            parms[index++].Value = this.AutoID;

            LinWon.Core.Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();

            return helper.ExecuteNonQuery(modifybaseinfo, parms); 
        }
        #endregion
    }

    /// <summary>
    /// 用户状态
    /// </summary>
    public enum UserState
    {
        /// <summary>
        /// 正常
        /// </summary>
        Normal,
        /// <summary>
        /// 锁定
        /// </summary>
        Locked,
        /// <summary>
        /// 删除
        /// </summary>
        Deleted
    }
}
